home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
8_3.lha
/
8_3
/
8_3_file.c
< prev
next >
Wrap
Text File
|
1993-08-08
|
909b
|
43 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ Exercise 8.3
/ Read in a file name, with error checking
include <stream.h>
include <stdio.h>
ifndef FILENAME_MAX /* DELETE */
define FILENAME_MAX 1024 /* DELETE */
endif /* DELETE */
nt read_filename(ostream &out, istream &in, char **val)
// set up flushing of the output stream
ostream *old = in.tie(&out);
// loop until we get something right
for ( ; ; out << "Try again\n")
{
out << "Type a file name: ";
// read a line, including the newline
char buf[FILENAME_MAX], c;
if (!cin.get(buf, FILENAME_MAX))
{
in.tie(old);
return 0;
}
in.get(c);
// check the value
if (!validfilename(buf))
continue;
// return the value, restoring the old tie first
*val = new char[strlen(buf) + 1];
strcpy(*val, buf);
in.tie(old);
return 1;
}